package tv.lomob.api;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class Lomob {
	private static final String BASE_URL = "http://api-lomob.ruangpanji.com/index.php/";
	private static String clientID = "";
	private static String cliendSecret = "";
	private static AsyncHttpClient client = new AsyncHttpClient();
	private static String token = "";
	private final static String API = "LOMOB_API";

	/*
	 * function for get absolute url use: param: string output: string use:
	 * getAbsoluteUrl("api/get_category")
	 */
	private static String getAbsoluteUrl(String relativeUrl) {
		return BASE_URL + relativeUrl;
	}

	public static void init(String clientId, String clientSecret) {

	}

	/*
	 * this function below about administration
	 */

	/*
	 * login function params: - username, username is email: String - password,
	 * user password: String
	 */
	public static void login(String username, String password,
			final LomobResponseHandler handler) {
		String url = "api/login";
		RequestParams params = new RequestParams();
		// add params here
		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {
						try {
							JSONObject objectResponse = new JSONObject(
									new String(response));
							if (objectResponse.optJSONObject("meta").optInt(
									"code") == 200) {
								// request success
								handler.onSuccess(objectResponse
										.optJSONObject("data"));
							} else {
								String message = objectResponse.optJSONObject(
										"meta").optString("message");
								handler.onError(message);
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {
						try {
							handler.onError(throwable.getMessage());
						} catch (Exception ex) {
							handler.onError(ex.getMessage());
						}
					}
				});
	}

	/*
	 * this function used for check if user is exist on lomob system response:
	 * JSONObject : {isregistered: true}
	 */
	public static void isRegistered(JSONObject user,
			final LomobResponseHandler handler) {
		String url = "api/login/is_registered/username/"
				+ user.optString("username");
		client.get(getAbsoluteUrl(url), new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] response) {
				try {
					JSONObject objectResponse = new JSONObject(new String(
							response));
					int code = objectResponse.optJSONObject("meta").optInt(
							"code");
					JSONObject result = new JSONObject();
					if (code == 200) {
						result.put("isregistered", true);
					} else {
						result.put("isregistered", false);
					}
					handler.onSuccess(result);

				} catch (Exception ex) {
					ex.printStackTrace();
					handler.onError(ex.getMessage());
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] error, Throwable throwable) {
				try {
					handler.onError(throwable.getMessage());
				} catch (Exception ex) {
					handler.onError(ex.getMessage());
				}
			}
		});
	}

	public static void authentificationFacebook(String userID, String iToken,
			final LomobResponseHandler handler) {
		try {
			String url = "api/login/facebook";
			JSONObject param = new JSONObject();
			param.put("token_fb", iToken);
			param.put("facebook_id", userID);
			RequestParams params = new RequestParams();
			params.add("param", param.toString());
			client.post(getAbsoluteUrl(url), params, new AsyncHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode, Header[] headers,
						byte[] response) {
					try {
						
						JSONObject objectResponse = new JSONObject(new String(
								response));
						
						if (objectResponse.optJSONObject("meta").optInt("code") == 200) {
							// request success
							handler.onSuccess(objectResponse.optJSONObject("data"));
						} else {
							String message = objectResponse.optJSONObject(
									"meta").optString("message");
							handler.onError(message);
						}
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}

				@Override
				public void onFailure(int statusCode, Header[] headers,
						byte[] error, Throwable throwable) {
					try {
						handler.onError(throwable.getMessage());
					} catch (Exception ex) {
						handler.onError(ex.getMessage());
					}
				}
			});
		} catch (Exception ex) {
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}

	}

	/*
	 * function below for social features
	 */

	public static void linkFacebook(JSONObject user,
			final LomobResponseHandler handler) {
		String url = "api/register/facebook";
		RequestParams params = new RequestParams();
		Log.i("LOMOB API", "send post: " + user.toString());
		params.add("param", user.toString());
		// add params here
		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {
						try {
							JSONObject objectResponse = new JSONObject(
									new String(response));
							if (objectResponse.optJSONObject("meta").optInt(
									"code") == 200) {
								// request success
								handler.onSuccess(objectResponse
										.optJSONObject("data"));
							} else {
								String message = objectResponse.optJSONObject(
										"meta").optString("message");
								handler.onError(message);
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {
						try {
							handler.onError(throwable.getMessage());
						} catch (Exception ex) {
							handler.onError(ex.getMessage());
						}
					}
				});
	}

	public static void requestPassword(String email,
			final LomobResponseHandler handler) {
		String url = "api/login/forgot";
		RequestParams params = new RequestParams();
		JSONObject objectParam = new JSONObject();

		try {
			objectParam.put("username", email);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// add params here
		params.add("param", objectParam.toString());

		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {
						try {
							JSONObject objectResponse = new JSONObject(
									new String(response));
							if (objectResponse.optJSONObject("meta").optInt(
									"code") == 200) {
								// request success
								handler.onSuccess(objectResponse
										.optJSONObject("meta"));
							} else {
								String message = objectResponse.optJSONObject(
										"meta").optString("message");
								handler.onError(message);
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {
						try {
							handler.onError(throwable.getMessage());
						} catch (Exception ex) {
							handler.onError(ex.getMessage());
						}
					}
				});
	}
	
	/*
	 * Get profile user
	 */
	public static void getProfile(String iToken, final LomobResponseHandler handler){
		try{
			String url = "api/user/index/" + iToken;
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse.optJSONObject("data"));
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								ex.printStackTrace();
							}

						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}

	/*
	 * Follow user params: user to follow : JSONOBject
	 */
	public static void followUser(String iToken, String idToFollow, final LomobResponseHandler handler) {
		try{
			String url = "api/follow/index/" + iToken;
			RequestParams params = new RequestParams();
			JSONObject userToFollow = new JSONObject();
			userToFollow.put("user_follow_id", idToFollow);
			params.add("param", userToFollow.toString());
			// add params here
			client.post(getAbsoluteUrl(url), params,
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse.optJSONObject("data"));
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
			
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}

	public static void unFollowUser(String iToken, String idToFollow, final LomobResponseHandler handler) {
		try{
			String url = "api/follow/unfollow/" + iToken;
			RequestParams params = new RequestParams();
			JSONObject userToFollow = new JSONObject();
			userToFollow.put("user_follow_id", idToFollow);
			params.add("param", userToFollow.toString());
			// add params here
			client.post(getAbsoluteUrl(url), params,
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse.optJSONObject("data"));
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
			
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}
	
	


	public static void loadFollowingUsers(LomobResponseHandler handler) {
		String url = "api/user/following/" + token;
		RequestParams params = new RequestParams();
		// add params here
		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {

					}
				});
	}

	public static void loadFollowerUsers(LomobResponseHandler handler) {
		String url = "api/user/follower/" + token;
		RequestParams params = new RequestParams();
		// add params here
		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {

					}
				});
	}
	
	public static void loadSuggestFriends(String iToken,  final LomobResponseHandler handler) {
		try{
			String url = "api/user/friendlist_fb/" + iToken;
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse);
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}

	public static void loadPopularUsers(String iToken, final LomobResponseHandler handler) {
		try{
			String url = "api/popular_user/index/" + iToken;
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse);
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}
	
	//function for get ftp account 
	public static void getFTPAccount(String iToken, final LomobResponseHandler handler) {
		try{
			String url = "api/user/ftp/" + iToken;
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									handler.onSuccess(objectResponse.optJSONObject("data"));
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}
	
	/*
	 * this function to get video categories and will return json array
	 */
	public static void getCategories(final LomobResponseHandler handler) {
		try{
			String url = "api/category";
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									JSONObject responseObject = new JSONObject();
									responseObject.put("categories", objectResponse.getJSONArray("data"));
									handler.onSuccess(responseObject);
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}
	
	/*
	 * this function to get restricted and will return json array
	 */
	
	public static void getRestricted(final LomobResponseHandler handler) {
		try{
			String url = "api/restricted";
			client.get(getAbsoluteUrl(url),
					new AsyncHttpResponseHandler() {

						@Override
						public void onSuccess(int statusCode, Header[] headers,
								byte[] response) {
							try {
								JSONObject objectResponse = new JSONObject(
										new String(response));
								if (objectResponse.optJSONObject("meta").optInt(
										"code") == 200) {
									// request success
									JSONObject responseObject = new JSONObject();
									responseObject.put("restricted", objectResponse.getJSONArray("data"));
									handler.onSuccess(responseObject);
								} else {
									String message = objectResponse.optJSONObject(
											"meta").optString("message");
									handler.onError(message);
								}
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
								ex.printStackTrace();
							}
						}

						@Override
						public void onFailure(int statusCode, Header[] headers,
								byte[] error, Throwable throwable) {
							try {
								handler.onError(throwable.getMessage());
							} catch (Exception ex) {
								handler.onError(ex.getMessage());
							}
						}
					});
		}catch(Exception ex){
			ex.printStackTrace();
			handler.onError(ex.getMessage());
		}
	}
	
	
	/*
	 * this video set profiling for video on demand
	 * 
	 */
	public static void setVideoProfile(String iToken, String title, String category, String restricted, String fileName,
			final LomobResponseHandler handler) {
		String url = "api/video/profiling_vod/" + iToken;
		RequestParams params = new RequestParams();
		JSONObject objectParam = new JSONObject();

		try {
			objectParam.put("title", title);
			objectParam.put("category", category);
			objectParam.put("restricted", restricted);
			objectParam.put("file", fileName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		// add params here
		params.add("param", objectParam.toString());

		client.post(getAbsoluteUrl(url), params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							byte[] response) {
						try {
							JSONObject objectResponse = new JSONObject(
									new String(response));
							if (objectResponse.optJSONObject("meta").optInt(
									"code") == 200) {
								// request success
								handler.onSuccess(objectResponse
										.optJSONObject("meta"));
							} else {
								String message = objectResponse.optJSONObject(
										"meta").optString("message");
								handler.onError(message);
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							byte[] error, Throwable throwable) {
						try {
							handler.onError(throwable.getMessage());
						} catch (Exception ex) {
							handler.onError(ex.getMessage());
						}
					}
				});
	}
	

}
